home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / oop_tp55.zip / PROROBOT.PAS < prev    next >
Pascal/Delphi Source File  |  1990-02-20  |  3KB  |  124 lines

  1. unit ProRobot;  { Listing 10-1 }
  2.  
  3. interface
  4.  
  5. uses Graph, Mouse, Crt;
  6.  
  7. const
  8.      PI : real = 3.14159;
  9.  
  10. type
  11.  
  12. Degrees = 0..359;
  13.  
  14. Point = object
  15.       X,Y : Integer;
  16.       procedure Init(NewX,NewY : Integer);
  17.       procedure Show;
  18.       procedure Hide;
  19.       end;
  20.  
  21.  
  22. Segment = object
  23.         Anchor      : Point;
  24.         BusyEnd     : Point;
  25.         Length      : integer;
  26.         Orientation : Real;
  27.         procedure Init( AnchorX, AnchorY, SegLen : Integer;
  28.                         Position : Degrees );
  29.         procedure MoveAxial( i : integer );
  30.         procedure Rotate( i : integer );
  31.         procedure Show;
  32.         procedure Hide;
  33.  
  34.         end;
  35.  
  36. function Distance( P1, P2 : Point ) : real;
  37.  
  38.  
  39. implementation
  40.  
  41. function Distance( P1, P2 : Point ) : real;
  42. begin
  43.      Distance :=
  44.        Sqrt( Sqr( P1.X - P2.X + 0.01) + Sqr( P1.Y - P2.Y + 0.01) );
  45. end;
  46.  
  47. procedure Point.Init( NewX, NewY : integer );
  48. begin
  49.      X := NewX;
  50.      Y := NewY;
  51. end;
  52.  
  53. procedure Point.Show;
  54. begin
  55.      Graph.Rectangle( X-8, Y-8, X+8, Y+8);
  56. end;
  57.  
  58. procedure Point.Hide;
  59. var TmpClr : integer;
  60. begin
  61.      TmpClr := Graph.GetColor;
  62.      Graph.SetColor(GetBkColor);
  63.      Show;
  64.      Graph.SetColor(TmpClr);
  65. end;
  66.  
  67. procedure Segment.Init( AnchorX, AnchorY, SegLen : Integer;
  68.                         Position : Degrees );
  69. var BX, BY : integer;
  70. begin
  71.      Anchor.Init( AnchorX, AnchorY );
  72.      Orientation :=  Position * PI / 180.0;
  73.      Length := SegLen;
  74.      BX := Round(Anchor.X + cos(Orientation)*SegLen);
  75.      BY := Round(Anchor.Y - sin(Orientation)*SegLen);
  76.      BusyEnd.Init( BX, BY );
  77. end;
  78.  
  79. procedure Segment.MoveAxial( i : integer );
  80. var D : real;
  81. begin
  82.      Hide;
  83.      D := Distance( Anchor, BusyEnd );
  84.      if D-i >= Length then
  85.         D := Length+i;
  86.      BusyEnd.X := Round(Anchor.X + cos(Orientation)*(D-i));
  87.      BusyEnd.Y := Round(Anchor.Y - sin(Orientation)*(D-i));
  88.      Show;
  89. end;
  90.  
  91. procedure Segment.Rotate( i : integer );
  92. begin
  93.      Hide;
  94.      Orientation := Orientation + ( i * ( PI / 90.0 ) );
  95.      Show;
  96. end;
  97.  
  98. procedure Segment.Show;
  99. var
  100.     D : real;
  101.     FX, FY : integer;
  102. begin
  103.      D := Distance( Anchor, BusyEnd );
  104.      BusyEnd.X := Round(Anchor.X + cos(Orientation)*D);
  105.      BusyEnd.Y := Round(Anchor.Y - sin(Orientation)*D);
  106.      FX := Round(BusyEnd.X - cos(Orientation)*Length);
  107.      FY := Round(BusyEnd.Y + sin(Orientation)*Length);
  108.      Graph.Line( FX, FY, BusyEnd.X, BusyEnd.Y );
  109.      Graph.Circle( BusyEnd.X, BusyEnd.Y, 2);
  110. end;
  111.  
  112. procedure Segment.Hide;
  113. var
  114.    TmpClr : integer;
  115.    LST : LineSettingsType;
  116. begin
  117.      TmpClr := Graph.GetColor;
  118.      Graph.SetColor(GetBkColor);
  119.      Show;
  120.      Graph.SetColor(TmpClr);
  121.  
  122. end;
  123.  
  124. end.